/*
 * @Author: your name
 * @Date: 2022-04-07 10:37:57
 * @LastEditTime: 2022-04-07 11:35:36
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: /comic-algorithm/chapter05/5.3.js
 */
/**
|--------------------------------------------------
| 设计 一个栈 出栈  进栈 取最小值  时间复杂度都是O（1）
  设计栈A  栈B （备份最小值 数组【】）
|--------------------------------------------------
*/
/**
 * initialize your data structure here.
 */
var MinStack = function () {
  this.mainStack = [];
  this.minStack = [];
};

/**
 * @param {number} val
 * @return {void}
 */
MinStack.prototype.push = function (val) {
  this.mainStack.push(val);
  // 如果最小栈为空，或者新元素小于或等于最小栈栈顶，则将新元素压入最小栈(备注：这里一定是小于等于  而非小于)
  if (!this.minStack.length || val <= this.getMin()) {
    this.minStack.push(val);
  }
};

/**
 * @return {void}
 */
MinStack.prototype.pop = function () {
  // 如果出栈元素和最小栈栈顶元素值相等，最小栈出栈
  if (this.mainStack[this.mainStack.length - 1] == this.getMin()) {
    this.minStack.pop();
  }
  return this.mainStack.pop();
};

/**
 * @return {number}
 */
MinStack.prototype.top = function () {
  return this.mainStack[this.mainStack.length - 1];
};

/**
 * @return {number}
 */
MinStack.prototype.getMin = function () {
  return this.minStack[this.minStack.length - 1];
};
